﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class PX : Form
    {
        int loginname = 0;
        string name = "";
        dbutils db = new dbutils();

        public void setValue(int loginname,string name)
        {
            this.loginname = loginname;
            this.name = name;
        }
        public PX()
        {
            InitializeComponent();
        }

        private void PX_Load(object sender, EventArgs e)
        {
            loadd();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql =
                "insert into px_table values('" + loginname + "','" + name + "','" + DateTime.Now.ToString("yyyy-MM-dd")+"','1999-1-1','1999-1-1',0)";
            if (db.insert(sql) == 0)
            {
                MessageBox.Show("error!");
                return;
            }
            MessageBox.Show("申请成功！");
            button1.Enabled = false;
            loadd();
        }

        private void loadd()
        {
            //先检查是否有资格参加培训
            //检查培训状态
            //检索分数
            //处理报名

            string sql1 =
                "select * from staff where loginname='" + loginname + "'";
            string re = db.select_one(sql1, 3);
            if (re == null)
            {
                MessageBox.Show("error!");
                return;
            }
            DateTime limitdate = DateTime.Parse("2017-1-1");
            DateTime entrydate = DateTime.Parse(re);
            label1.Text = limitdate.ToString("yyyy-MM-dd") + "之后的人员可以参加培训。";
            label2.Text = "您的入职时间为" + entrydate.ToString("yyyy-MM-dd");
            if (entrydate.Year <= limitdate.Year)
            {
                if (entrydate.Year < limitdate.Year)
                {
                    label2.Text += "，不可参加培训！";
                    button1.Enabled = false;
                    return;
                }
                if (entrydate.DayOfYear < limitdate.DayOfYear)
                {
                    label2.Text += "，不可参加培训！";
                    button1.Enabled = false;
                    return;
                }
            }
            label2.Text += "，可以参加培训！";


            string sql2 =
                "select * from px_table where staffid='" + loginname + "'";
            string re2 = db.select_multi(sql2, 6);
            if (re2 == null)
            {
                label3.Text = "您尚未申请参加培训,您可以";
                return;
            }
            label3.Text = "您已提交培训申请";
            button1.Enabled = false;
            string[] re3 = re2.Split('+');
            MessageBox.Show(re2);
            int state = int.Parse(re3[5]);

            if (state == 0)
            {
                label4.Text += "尚未开始";
                label6.Text += re3[2];
                return;
            }
            if (state == 1)
            {
                label4.Text += "正在进行";
                label6.Text += re3[2];
                label5.Text += re3[3];
                return;
            }
            if (state == 2)
            {
                label4.Text += "培训结束";
                label6.Text += re3[2];
                label5.Text += re3[3];
                label7.Text += re3[4];
                string sql3 =
                    "select * from px_cj_table where staffid='" + loginname + "'";
                string re4 = db.select_one(sql3, 2);
                if (re4 == null)
                {
                    MessageBox.Show("error!");
                    return;
                }
                label8.Text += re4;
                return;
            }

        }

        
    }
}
